﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TCHSHue.BLL;
using TCHSHue.DAL;
namespace TCHSHue.Modules.AdminControl.BaiViet
{
    public partial class ctrBaiVietList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int pCurrent = getPageCurrent();
                NapDanhSachTheLoai();
                NapDanhSachTinhTrang();
                NapDanhSachBaiViet(ddlTheLoai.SelectedValue, ddlTinhTrang.SelectedValue, pCurrent);
            }
        }

        void NapDanhSachTheLoai()
        {
            var ls = TheLoaiBLL.getList();
            ls.Insert(0, new TheLoai() { maTheLoai = -1, tenTheLoai = "Tất cả" });
            ddlTheLoai.DataSource = ls;
            ddlTheLoai.DataTextField = "tenTheLoai";
            ddlTheLoai.DataValueField = "maTheLoai";
            ddlTheLoai.DataBind();
        }

        void NapDanhSachTinhTrang()
        {
            List<TinhTrang> ls = new List<TinhTrang>();
            ls.Add(new TinhTrang() { Ma = -1, TenHienThi = "Tất cả" });
            ls.Add(new TinhTrang() { Ma = (int)TinhTrangBaiViet.ChuaDuyet, TenHienThi = "Chưa duyệt" });
            ls.Add(new TinhTrang() { Ma = (int)TinhTrangBaiViet.DaDuyet, TenHienThi = "Đã duyệt" });
            ddlTinhTrang.DataSource = ls;
            ddlTinhTrang.DataTextField = "TenHienThi";
            ddlTinhTrang.DataValueField = "Ma";
            ddlTinhTrang.DataBind();
        }

        protected void RedirectEdit(object sender, CommandEventArgs e)
        {
            Response.Redirect("~/Modules/Admin.aspx?mode=chinhbaiviet&id=" + e.CommandArgument.ToString());
        }

        protected void Delete(object sender, CommandEventArgs e)
        {
            if (!BaiVietBLL.delete(e.CommandArgument.ToString()))
                Page.RegisterClientScriptBlock("XoaBaiViet", "<SCRIPT Language='JavaScript'> alert('Không thể xóa bài viết này'); </SCRIPT>");
            else
                NapDanhSachBaiViet(ddlTheLoai.SelectedValue, ddlTinhTrang.SelectedValue, getPageCurrent());
        }

        private void NapDanhSachBaiViet(string TheLoai, string TinhTrang, int pCurrent)
        {
            var theLoai = int.Parse(TheLoai);
            var tinhTrang = int.Parse(TinhTrang);
            var ls = new List<GridBaiViet>();
            int tsBai = 0;
            if (theLoai != -1 && tinhTrang != -1)
            {
                ls = BaiVietBLL.getListTheoTrang(theLoai, Hang.PAGESIZE, pCurrent, tinhTrang);
                tsBai = BaiVietBLL.getToTal(theLoai, tinhTrang);
            }
            else if (theLoai != -1)
            {
                ls = BaiVietBLL.getListTheoTrang(theLoai, Hang.PAGESIZE, pCurrent);
                tsBai = BaiVietBLL.getToTal(theLoai);
            }
            else if (tinhTrang != -1)
            {
                ls = BaiVietBLL.getListTheoTrang("", tinhTrang, Hang.PAGESIZE, pCurrent);
                tsBai = BaiVietBLL.getToTal("", tinhTrang);
            }
            else
            {
                ls = BaiVietBLL.getListTheoTrang(Hang.PAGESIZE, pCurrent);
                tsBai = BaiVietBLL.getToTal();
            }
            gridBaiViet.DataSource = ls;
            gridBaiViet.DataBind();
            PhanTrang(tsBai, pCurrent, "../Modules/Admin.aspx?mode=baiviet&");
        }

        protected void bntLoc_Click(object sender, EventArgs e)
        {
            NapDanhSachBaiViet(ddlTheLoai.SelectedValue, ddlTinhTrang.SelectedValue, getPageCurrent());
        }

        int getPageCurrent()
        {
            var pageIndex = Request.QueryString["pageIndex"];
            int temp;
            int pCurrent = 1;
            if (!int.TryParse(pageIndex, out temp))
                pCurrent = 1;
            else
                pCurrent = int.Parse(pageIndex);
            return pCurrent;
        }

        void PhanTrang(int total, int pCurrent, string baseLink)
        {
            Paging paGing = new Paging(Hang.PAGESIZE);
            string resultPaging = paGing.CreatePaging(total, pCurrent, baseLink);
            if (string.IsNullOrEmpty(resultPaging))
                divPaging.Visible = false;
            else
            {
                divPaging.InnerHtml = resultPaging;
            }
        }
    }
}